package it.patterns.behavioral.chainresponsibility;

/**
 * 
 * Avoid coupling the sender of a request to its receiver by giving more than
 * one object a chance to handle the request. Chain the receiving object and
 * pass the request along the chain until an object handles it.
 * 
 * More than one object may handle a request, and the handler isn't known a
 * priori. You can use this pattern if you want to issue a request to one of
 * several objects without specifying the receiver explicitly. This set of
 * objects can be specified dynamically.
 * 
 * Consequences:
 * <ul>
 * <li>Reduce coupling: the patterns frees an object from knowing which other
 * object handles a request.</li>
 * <li>Added flexibility in assigning responsibilities to objects (also at
 * run-time).</li>
 * <li>Receipt isn't guaranteed.</li>
 * </ul>
 * 
 * <img src="ChainResponsibility.jpg" alt="Pattern Structure"/>
 * 
 * @author Luca D'Onofrio
 * 
 * @category Behavioral Pattern
 * @see "Design Patterns - Element of Reusable Object Oriented Software (Gamma,Helm, Johnson, Vlissides)"
 * 
 * @version 1.0
 * 
 */
public abstract class ChainOfResponsibility {
	private ChainOfResponsibility successorHandler;

	public void setSuccessorHandler(ChainOfResponsibility successorHandler) {
		this.successorHandler = successorHandler;
	}

	public String handleRequest(int value) {
		return forwardRequest(value);
	}

	protected String forwardRequest(int value) {
		if (successorHandler != null)
			return successorHandler.handleRequest(value);
		else
			return "No handler found !";
	}
}
